/*
 * WaveListCellRenderer.java
 * 
 * last update: 14.01.2010 by Stefan Saru
 * 
 * author:	Lucian Ghita Felix(felix@ulise.cs.pub.ro)
 * 			Power (power@kermit.cs.pub.ro)
 * 			Alec (alecu@kermit.cs.pub.ro)
 *
 * Obs: N/A
 */


package ide;

import javax.swing.*;
import java.awt.*;
import middle.*;
import engine.main;
import engine.VeriParser;

@SuppressWarnings("serial")
class WaveListCellRenderer extends JComponent implements ListCellRenderer {
	WaveView waveView;
	VariableHistory vh;
	boolean showFirstLine = false;
	boolean showSecondLine = false;

	int firstLinePosition = 0;  //in timeUnituri
	int secondLinePosition = 0;


	/**
	 * Renders a single wave into the viewer.
	 */
	public WaveListCellRenderer(WaveView wvv) {
		super();
		this.waveView = wvv;
		setMinimumSize(new Dimension(300, 20));
		setPreferredSize(new Dimension(300, 20));
	}

	public java.awt.Component getListCellRendererComponent(      
			JList list,
			Object value,           // value to display //VariableHistory typpe.
			int index,              // cell index             //nu   
			boolean isSelected,     // is the cell selected   //nu
			boolean cellHasFocus)   // the list and the cell have the focus  //nu   
	{

		this.vh = (VariableHistory) value;
		//String s = value.toString();        setText(s);
		//setIcon((s.lengt.h > 10) ? longIcon : shortIcon);      return this;
		//String val = ((VariableHistory)value).getStringValue(long timeUnit);

		return this;
	}

	public void paint(java.awt.Graphics g) {
		g.setColor(Color.black);
		g.fillRect(0, 0, getWidth(), getHeight());
		g.setColor(Color.yellow);
		int vType = vh.getType();
		int width = waveView.waveMinimumUnitWeigth + waveView.actualZoom;
		int height = getHeight();
		String lastVal = "X";

		if (vType == VariableHistory.SCALAR) {
			for(int i = 0; i < waveView.simulationTime; i ++) {
				String val = VeriParser.unUnderscore(vh.getStringValue(i));

				if (val.equalsIgnoreCase("X")) {
					if (lastVal.equalsIgnoreCase("1"))
						g.drawLine(i * width, 2, i * width, height / 2);
					if (lastVal.equalsIgnoreCase("0"))
						g.drawLine(i * width, height - 2, i * width, height / 2);

					g.drawLine(i * width, height / 2, (i + 1) * width, height / 2);

				} else if (val.equalsIgnoreCase("0")) {
					if (lastVal.equalsIgnoreCase("1"))
						g.drawLine(i * width, 2, i * width, height - 2);
					if (lastVal.equalsIgnoreCase("X"))
						g.drawLine(i * width, height /2, i * width, height - 2);

					g.drawLine(i * width, height - 2, (i + 1) * width, height - 2);

				} else if (val.equalsIgnoreCase("1")) {

					if (lastVal.equalsIgnoreCase("0"))
						g.drawLine(i * width, height - 2, i * width, 2);
					if (lastVal.equalsIgnoreCase("X"))
						g.drawLine(i * width, height /2, i * width,  2);

					g.drawLine(i * width,2, (i + 1) * width, 2);

				} else if (val.equalsIgnoreCase("Z")) {
					g.fillRect(i * width, 1, width, height - 1);
				} else System.out.println("Fatal: illegal value in VariableHistory.getValue : " + val + " size is: " + val.length());

				lastVal = val;
			}
			//trage linia verticala
			if(waveView.isLine){
				g.setXORMode(Color.red);
				g.drawLine(waveView.xPos,0,waveView.xPos,height); 
			}

			return;
		}

		if (vType == VariableHistory.VECTOR) {
			int firstTime = 0;
			for(int i = 0; i < waveView.simulationTime; i ++) {
				String val = vh.getStringValue(i);
				//System.out.println("val is:" + val);
				if(val == null)System.out.println("a VarHistory returned null: " + vh);
				if (val.equalsIgnoreCase(lastVal)) {
					g.drawLine(i * width - 2, 4, (i+1) * width - 2, 4);
					g.drawLine(i * width - 2, height - 4, (i+1) * width - 2, height - 4);
				} else {
					if (i > 0) g.drawString(lastVal,
							width * ((i-1) + firstTime) / 2 + 5,
							height / 2);
					firstTime = i;
					g.drawLine(i * width - 2, 4, i * width + 2, height - 4);
					g.drawLine(i * width + 2, 4, i * width - 2, height - 4);
					g.drawLine(i * width + 2, 4, (i+1) * width - 2, 4);
					g.drawLine(i * width + 2, height - 4, (i+1) * width - 2, height - 4);
				}
				lastVal = val;
			}

			//trage linia verticala
			if(waveView.isLine){
				g.setXORMode(Color.red);
				g.drawLine(waveView.xPos,0,waveView.xPos,height); 
			}

			return;
		}

	}
}
